﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.ThietBiPhongHocService;
namespace eTMS.Controllers{
public class ThietBiPhongHocControllerBase : CommonController{
	public ThietBiPhongHocControllerBase()
		: base()
	{	
		this.dao = new ThietBiPhongHocServiceClient();
	}
	~ThietBiPhongHocControllerBase(){
		dao.Close();
	}
	private DataTable _Table;
	private ThietBiPhongHocEntity oThietBiPhongHoc;	
	public ThietBiPhongHocEntity ThietBiPhongHocObject{
		get{
			return oThietBiPhongHoc;
		}
		set{
			oThietBiPhongHoc=value;
		}
	}
	protected ThietBiPhongHocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSThietBiPhongHoc(false);			
			return _Table;
		}
	}
	public void LoadAllDSThietBiPhongHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oThietBiPhongHoc==null)return false;
		bool result=true;
		try{
			oThietBiPhongHoc = dao.SaveRet(oThietBiPhongHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListThietBiPhongHoc.Add(oThietBiPhongHoc);
		}
		return result;
	}
	public bool Update(){
		if(oThietBiPhongHoc==null)return false;
		return dao.Update(oThietBiPhongHoc);
	}
	public bool Delete(){
		if(oThietBiPhongHoc==null)return false;
		bool retvalue = dao.Delete(oThietBiPhongHoc);
		if(retvalue){			
			_ListThietBiPhongHoc.Remove(oThietBiPhongHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 ThietBiPhongHoc_ID){
		bool retvalue = dao.DeleteByID(ThietBiPhongHoc_ID);		
		if(retvalue){
			ThietBiPhongHocEntity oDeleted=FindThietBiPhongHoc(ThietBiPhongHoc_ID);
			if(oDeleted!=null){
				_ListThietBiPhongHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oThietBiPhongHoc = new ThietBiPhongHocEntity();
	}
    //Copy data from data row in grid to object
    public ThietBiPhongHocEntity MapDataRowToObject(DataRow row){
		ThietBiPhongHocEntity o = new ThietBiPhongHocEntity();
		 if(!row.IsNull("LoaiThietBi_ID"))
			o.LoaiThietBi_ID=(Int32)(row["LoaiThietBi_ID"]);
		 if(!row.IsNull("PhongHoc_ID"))
			o.PhongHoc_ID=(Int32)(row["PhongHoc_ID"]);
		 if(!row.IsNull("ThietBiPhongHoc_ID"))
		    o.ThietBiPhongHoc_ID=(Int32)(row["ThietBiPhongHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(ThietBiPhongHocEntity o,DataRow row){
		row["LoaiThietBi_ID"]=o.LoaiThietBi_ID;
		row["PhongHoc_ID"]=o.PhongHoc_ID;
		row["ThietBiPhongHoc_ID"]=o.ThietBiPhongHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.ThietBiPhongHocObject,row);
    }
	public void LoadObject(Int32 ThietBiPhongHoc_ID){
		ThietBiPhongHocEntity oLoad=FindThietBiPhongHoc(ThietBiPhongHoc_ID);
		if(oLoad==null){
			oThietBiPhongHoc=this.LoadThietBiPhongHoc(ThietBiPhongHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oThietBiPhongHoc.IsNullThietBiPhongHoc_ID;
			if(!bNotIsNull){
				this.ListThietBiPhongHoc.Add(oThietBiPhongHoc);
			}
		}
		else{
			oThietBiPhongHoc=oLoad;
		}
	}
	#region CachedList ThietBiPhongHoc
	private static List<ThietBiPhongHocEntity> _ListThietBiPhongHoc=new List<ThietBiPhongHocEntity>();
    public List<ThietBiPhongHocEntity>ListThietBiPhongHoc{
		get{
			return _ListThietBiPhongHoc;
		}
	}
	public static List<ThietBiPhongHocEntity>ListThietBiPhongHocStatic{
		get{
			return _ListThietBiPhongHoc;
		}
	}
	public ThietBiPhongHocEntity FindThietBiPhongHoc(Int32 ThietBiPhongHoc_ID){
		ThietBiPhongHocEntity oResult=this.ListThietBiPhongHoc.Find(delegate(ThietBiPhongHocEntity oFind){
			bool result=true; 
			result&=oFind.ThietBiPhongHoc_ID.Equals(ThietBiPhongHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllThietBiPhongHoc(bool clearCache){
		if(_ListThietBiPhongHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		ThietBiPhongHocServiceClient dao = new ThietBiPhongHocServiceClient();
		_ListThietBiPhongHoc=new List<ThietBiPhongHocEntity>(dao.SelectAll());
		dao.Close();
	}
	public ThietBiPhongHocEntity LoadThietBiPhongHoc(Int32 ThietBiPhongHoc_ID){			
		ThietBiPhongHocEntity o=dao.SelectOne(ThietBiPhongHoc_ID);		
		return o;
	}
	#endregion
}
}
